package com.wang.util;

import java.sql.SQLException;

/**
 * Created by wangq on 2021/7/11.
 *
 * 事务管理器
 */
public class TransactionManager {
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

//    private TransactionManager() {
//    }
//
//    // 使用单例模式的饿汉模式
//    private static final TransactionManager TRANSACTION_MANAGER = new TransactionManager();
//
//    public static TransactionManager getInstance() {
//        return TRANSACTION_MANAGER;
//    }

    /**
     * 开启事务
     *
     * @throws SQLException
     */
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    /**
     * 提交事务
     *
     * @throws SQLException
     */
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }

    /**
     * 回滚事务
     *
     * @throws SQLException
     */
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}
